<style scoped>
</style>
<template>
    <div class="mui-content books-list-content" id="content">

        微信授权页面

    </div>
</template>
<script>

    import api from '@/fetch/api';
    import axios from 'axios';
    import qs from 'qs';
    let wx = process.env.WX_CONFIG;


    export default {
        data () {
            return {
                subjects: {},
            }
        },
        methods: {
            getToken(code){
                let reqUrl = 'https://api.weixin.qq.com/sns/oauth2/access_token?';
                let params = {
                    appid: wx.appId,
                    secret: wx.appSecret,
                    code: code,
                    grant_type: 'authorization_code'
                };

                let options = {
                    method: 'get',
                    url: reqUrl+qs.stringify(params)
                };
                console.log(options.url);
//            return new Promise((resolve, reject) => {
//                request(options, function (err, res, body) {
//                    if (res) {
//                        resolve(body);
//                    } else {
//                        reject(err);
//                    }
//                })
//            });

                return new Promise((resolve, reject) => {
                    axios({
                        method: 'get',
                        url: reqUrl,
                        params: qs.stringify(params)
                    }).then((response) => {
                        console.log(JSON.stringify(response));
                        resolve(response.data);
                    })
                });
            },
            getUserInfo(AccessToken, openId) {
                let reqUrl = 'https://api.weixin.qq.com/sns/userinfo?';
                let params = {
                    access_token: AccessToken,
                    openid: openId,
                    lang: 'zh_CN'
                };

                return new Promise((resolve, reject) => {
                    axios({
                        method: 'get',
                        url: reqUrl,
                        params: qs.stringify(params)
                    }).then((response) => {
                        console.log(JSON.stringify(response));
                        resolve(response.data);
                    })
                });
            }
        },
        mounted(){
            let $this = this;

            console.log(this.$route.query);
            $this.getToken($this.$route.query.code);


        }
    }

</script>
